package com.qf.openapi.web.master.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.openapi.web.master.dao.ApiSystemParamterDao;
import com.qf.openapi.web.master.entity.ApiSystemParam;
import com.qf.openapi.web.master.service.ApiSystemParamterService;
import com.qf.openapi.web.master.vo.ResultVo;
import com.qf.openapi.web.master.vo.TableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public class ApiSystemParamterServiceImpl implements ApiSystemParamterService {
    @Autowired
    private ApiSystemParamterDao apiSystemParamterDao;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public TableVo<ApiSystemParam> getApiSystemParamterByContdition(Integer page, Integer limit, ApiSystemParam apiSystemParam) {
        PageHelper.startPage(page, limit);
        List<ApiSystemParam> list = apiSystemParamterDao.findApiSystemParamterByContdition(apiSystemParam);
        PageInfo<ApiSystemParam> pageInfo = new PageInfo<>(list);
        TableVo<ApiSystemParam> tableVo = null;
        if (list != null) {
            tableVo = new TableVo<>(0, "查询成功", (int) pageInfo.getTotal(), list);
        } else {
            tableVo = new TableVo<>(-1, "查询失败", 0, null);
        }
        return tableVo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo deleteApiSystemParamter(Integer[] ids) {
        Integer flag = apiSystemParamterDao.deleteApiSystemParamterDaoByIds(ids);
        ResultVo resultVo = null;
        if (flag > 0) {
            resultVo = new ResultVo(1, "删除成功");
        } else {
            resultVo = new ResultVo(-1, "删除失败");
        }
        return resultVo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo addApiSystemParamter(ApiSystemParam apiSystemParam) {
        Integer flag = apiSystemParamterDao.addApiSystemParamter(apiSystemParam);
        ResultVo resultVo = null;
        if (flag > 0) {
            resultVo = new ResultVo(1, "增加成功");
        } else {
            resultVo = new ResultVo(-1, "增加失败");
        }
        return resultVo;
    }

    @Override
    public ResultVo updateApiSystemParamter(ApiSystemParam apiSystemParam) {
        Integer flag = apiSystemParamterDao.updateApiSystemParamter(apiSystemParam);
        ResultVo resultVo = null;
        if (flag > 0) {
            resultVo = new ResultVo(1, "修改成功");
        } else {
            resultVo = new ResultVo(-1, "修改失败");
        }
        return resultVo;
    }
}
